//
//  OtherInformationCtrl.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/1/28.
//  Copyright © 2016年 game. All rights reserved.
//

#import "OtherInformationCtrl.h"
#import "OtherHeadView.h"
#import "SpaceModel.h"
#import "SelectionCell.h"
#import "SeleCtContrl.h"
#import "LoginController.h"
#import "thread_listModel.h"
#import "SpacePagerModel.h"
#import "LoginController.h"
#import "SayHilloViewController.h"
#import "RootTableview.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"
#define IMghight 200

@interface OtherInformationCtrl ()<UITableViewDelegate,UITableViewDataSource,SelectionCellDeleget>

@property (nonatomic,strong) RootTableview *tableview;
@property (nonatomic,strong) OtherHeadView *tabHead;
@property (nonatomic,strong) UIImageView *bacImg;
@property (nonatomic,strong) SpaceModel *model;
@property (nonatomic,strong) NSMutableArray *dataAry;
@property (nonatomic,strong) NSMutableDictionary *cellHightDic;
@end

@implementation OtherInformationCtrl

-(NSMutableDictionary *)cellHightDic{
    if (!_cellHightDic) {
        _cellHightDic = [NSMutableDictionary dictionary];
    }
    return _cellHightDic;
}

-(NSMutableArray *)dataAry{
    if (!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = YES;
}

#pragma mark--加好友
- (IBAction)AddFireld:(id)sender {
    
    if ([self.app.isLogin isEqualToString:@"YES"]) {
        
        [[Manager sharedManager]RequestWhithURL:GetFormhash AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
            if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
                
                [self AddFriendWhithDic:@{@"uid":self.uid,@"formhash":responseObject[@"data"][@"formhash"]}];
            }
        }];
        
    }else{
        LoginController *login = [LoginController new];
        [self.navigationController pushViewController:login animated:YES];
    }
}

-(void)AddFriendWhithDic:(NSDictionary *)dic{
    [[Manager sharedManager]ResponseWhithURL:AddMyLickFriend AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
        
        if (!error) {
            [Manager GetHudWhithText:responseObject[@"message"] AndFatherView:self.view];
        }else{
        
            [Manager GetHudWhithText:@"添加好友失败" AndFatherView:self.view];
        }
    }];
}

#pragma mark--打招呼
- (IBAction)sayHillo:(id)sender {
    
    if ([self.app.isLogin isEqualToString:@"YES"]) {
        SayHilloViewController *hillo = [SayHilloViewController new];
        hillo.otherId = self.uid;
        [self.navigationController pushViewController:hillo animated:YES];

    }else{
        LoginController *login = [LoginController new];
        [self.navigationController pushViewController:login animated:YES];
    }
}

-(void)viewDidLoad{
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIButton *backBtn = [self CreatBntAddView:self.view WhithRect:CGRectMake(10, 30, 35, 30) AndType:UIButtonTypeCustom AndBntImgName:@"back-Whrit" AndTitle:@"" Font:10];
    backBtn.backgroundColor  =[UIColor lightGrayColor];
    backBtn.alpha = 0.7;
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 15);
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
   
    self.tableview.tableHeaderView = self.tabHead;
    [self.tableview insertSubview:self.bacImg atIndex:0];
    [self.view bringSubviewToFront:backBtn];
    
    [self AchiveDataWhithPage:1];
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
/**
 *  下拉放大的图片
 */
-(UIImageView *)bacImg{
    if (!_bacImg) {
    _bacImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, IMghight)];

        [_bacImg sd_setImageWithURL:[NSURL URLWithString:@"http://www.0739i.com.cn/data/attachment/portal/201603/09/120158ksjocrjsoohrmhtg.jpg"]];
        _bacImg.contentMode = UIViewContentModeScaleAspectFill;
      
        [self.view addSubview:_bacImg];
    }
    return _bacImg;
}
#pragma mark--显示用户名头像的tableview头部
-(OtherHeadView *)tabHead{
    if (!_tabHead) {
        _tabHead = [[NSBundle mainBundle]loadNibNamed:@"OtherHeadView" owner:nil options:nil].lastObject;
       _tabHead.frame =  CGRectMake(0, 0, screen_width, IMghight);
    }

    return _tabHead;
}

-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[RootTableview alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-50) style:UITableViewStylePlain];
        _tableview.backgroundColor =  tabGropBackColor;
        _tableview.dataSource = self;
        _tableview.delegate = self;
        _tableview.separatorStyle = 0;
      
        [_tableview registerNib:[UINib nibWithNibName:@"SelectionCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(LoadMorelData)];
        
        _tableview.footer.automaticallyHidden = YES;

        [self.view sendSubviewToBack:_tableview];
        [self.view addSubview:_tableview];
    }
    return _tableview;
}
#pragma mark---上拉加载
-(void)LoadMorelData{

    SpacePagerModel *pageModel  = self.model.pager;
    
    if (self.model.pager) {
        
        if ((pageModel.cur_page.integerValue+1)==pageModel.total_page.integerValue) {
            [_tableview.footer noticeNoMoreData];
        }else{
            [self AchiveDataWhithPage:pageModel.cur_page.integerValue+1];

        }
        
    }else{
        [_tableview.footer noticeNoMoreData];
    
    }
   
    
}

-(void)AchiveDataWhithPage:(NSInteger )page{
    
    [[Manager sharedManager]RequestWhithURL:[NSString stringWithFormat:SpaceUrl,self.uid,(long)page] AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {

//        NSLog(@"%@",responseObject);
        
        if (!error) {
            self.model = [[SpaceModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
            self.tabHead.Membermodel = self.model.member;
            [self.dataAry addObjectsFromArray:self.model.thread_list];
             _tableview.isShowImage = YES;
            [UIView transitionWithView:self.tableview duration:0.7 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
                [_tableview reloadData];
            } completion:nil];
            [_tableview.footer endRefreshing];
           
        }else{
            [_tableview.footer endRefreshing];
            _tableview.isShowImage = NO;
            [_tableview reloadData];
            [_tableview.footer noticeNoMoreData];
        }
       
    }];
}


-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataAry.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 0.1+[[self.cellHightDic valueForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]] floatValue];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SelectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (self.dataAry.count!=0) {
        cell.model = self.dataAry[indexPath.row];
     
        cell.deleget = self;
        
        [self.cellHightDic setValue:[NSString stringWithFormat:@"%f",cell.height] forKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
    }
    return cell;
}
#pragma mark--SelectionCell中的代理方法
-(void)CommentSelect:(NSString *)tid Row:(NSInteger)row{
    NSString *url = [NSString stringWithFormat:MainSelet,tid];
    
    SeleCtContrl *str = [SeleCtContrl new];
    str.tid = tid;
    str.url = url;
    str.isSlide = YES;
    str.rowOfCell = row;
    [self.navigationController pushViewController:str animated:YES];
}

-(void)UserSpaceClick:(NSString *)uid{
    OtherInformationCtrl *space = [OtherInformationCtrl new];
    space.uid = uid;
    [self.navigationController pushViewController:space animated:YES];
}

-(void)PramisNoLogin{
    LoginController *login = [LoginController new];
    [self.navigationController pushViewController:login animated:YES];
}
-(void)PraimSelect:(NSInteger)section{
    
    thread_listModel *model = self.model.thread_list[section];
    model.isZan = YES;
}

-(void)PraimFile:(NSString *)message{

    [Manager GetHudWhithText:message AndFatherView:self.view];
}

#pragma mark--tableview点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    SelectionCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *url = [NSString stringWithFormat:MainSelet,cell.tid];
    
    SeleCtContrl *str = [SeleCtContrl new];
    str.tid = cell.tid;
    str.url = url;
    str.isSlide = NO;
    str.rowOfCell = indexPath.row;
    [self.navigationController pushViewController:str animated:YES];
}
#pragma mark--下拉图片变大
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
  
    CGFloat offsetH = -IMghight *0 - offsetY;
    CGRect frame = self.bacImg.frame;
    frame.size.height = IMghight + offsetH;
    frame.origin.y = -offsetH;
    self.bacImg.frame = frame;
}

@end
